{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "torrentTOmega_gdrive.ipynb",
      "provenance": [],
      "collapsed_sections": [
        "6wktcwkWtHH0",
        "xa8RqAm2wozX",
        "c3cOMk38t8Zm",
        "yOnMe4O3jtXf",
        "kSvUMUuTPX7r"
      ],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/biplobsd/Google-Colab-CloudTorrent/blob/master/torrentTOmega_gdrive.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6wktcwkWtHH0",
        "colab_type": "text"
      },
      "source": [
        "# <img src='https://i.imgur.com/RhSYbDW.png' height=\"55\" alt=\"Peerflix-server\" />"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dMnsw5KKDT6E",
        "colab_type": "code",
        "cellView": "form",
        "colab": {}
      },
      "source": [
        "#@markdown <h3>⬅️ Click Here to START server</h3>\n",
        "#@markdown <br><center><img src='https://i.imgur.com/RhSYbDW.png' height=\"100\" alt=\"netdata\"/></center>\n",
        "#@markdown <center><h3>☁️ Cloud Torrent: a self-hosted remote torrent client</h3></center><br>\n",
        "import os, sys, subprocess; from IPython.display import clear_output\n",
        "\n",
        "!pkill ssh\n",
        "print(\"Loading ...\")\n",
        "if os.path.isfile(\"/usr/local/bin/cloud-torrent\") == False:\n",
        "  os.system('curl https://i.jpillora.com/cloud-torrent! | bash')\n",
        "else:\n",
        "  !pkill cloud-torrent\n",
        "clear_output(wait=True)\n",
        "\n",
        "#Forwarding public server by serveo.net\n",
        "!nohup ssh -o StrictHostKeyChecking=no -R 80:localhost:4444 serveo.net > link.out &\n",
        "\n",
        "#Opening cloud-torrent in background\n",
        "!nohup cloud-torrent --port 4444 --title \"Cloud Torrent\" --log &\n",
        "clear_output(wait=True)\n",
        "print(os.popen('tail -n 2 link.out').read())\n",
        "\n",
        "#MEGAcmd installing\n",
        "if os.path.isfile(\"/usr/bin/mega-cmd\") == False:\n",
        "  print(\"Installing Mega ...\")\n",
        "  os.system('sudo apt-get -y update && sudo apt-get -y install libmms0 libc-ares2 libc6 libcrypto++6 libgcc1 libmediainfo0v5 libpcre3 libpcrecpp0v5 libssl1.1 libstdc++6 libzen0v5 zlib1g apt-transport-https && sudo curl -sL -o/var/cache/apt/archives/MEGAcmd.deb https://mega.nz/linux/MEGAsync/Debian_9.0/amd64/megacmd-Debian_9.0_amd64.deb && sudo dpkg -i /var/cache/apt/archives/MEGAcmd.deb && sudo ldconfig')\n",
        "  print(\"Mega is installed.\")\n",
        "else:\n",
        "  !pkill mega-cmd\n",
        "#Google Drive CLI Client\n",
        "if os.path.isfile(\"/usr/local/bin/gdrive\") == False:\n",
        "  print(\"Google Drive Cli Client installing ...\")\n",
        "  !wget -q -c -nc https://github.com/gdrive-org/gdrive/releases/download/2.1.0/gdrive-linux-x64\n",
        "  !mv gdrive-linux-x64 /usr/local/bin/gdrive\n",
        "  !sudo chmod +x /usr/local/bin/gdrive\n",
        "  !rm -f /content/gdrive-linux-x64\n",
        "  print(\"Google drive client installed\")\n",
        "else:\n",
        "  !pkill gdrive\n",
        "print(\"All Done.\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xa8RqAm2wozX",
        "colab_type": "text"
      },
      "source": [
        "# <img src='https://camo.githubusercontent.com/95b0ba2a237ee846b3bb8f45954124eb9cba4ebc/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f617361706163682f70656572666c69782d736572766572406d61737465722f6170702f696d616765732f6c6f676f2e737667' height=\"25\" alt=\"Peerflix-server\" /> PEERFLIX-SERVER"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8VeZvm-Ywr9G",
        "colab_type": "code",
        "cellView": "both",
        "colab": {}
      },
      "source": [
        "#@markdown <h3>⬅️ Click Here to START server</h3>\n",
        "#@markdown <br><center><img src='https://camo.githubusercontent.com/95b0ba2a237ee846b3bb8f45954124eb9cba4ebc/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f617361706163682f70656572666c69782d736572766572406d61737465722f6170702f696d616765732f6c6f676f2e737667' height=\"200\" alt=\"netdata\"/></center>\n",
        "#@markdown <center><h3>PEERFLIX-SERVER<br />Streaming torrent client for node.js with web ui.</h3></center><br>\n",
        "import sys, subprocess, string, os, urllib.request, json, time, getpass; from IPython.display import clear_output\n",
        "!pkill autossh\n",
        "!pkill nohup\n",
        "!pkill peerflix-server\n",
        "\n",
        "configPath = r\"/root/.config/peerflix-server/config.json\"\n",
        "#CREATE THE CONFIG\n",
        "data = r\"\"\"\n",
        "{ \n",
        "   \"path\": \"/content/peerflix\",\n",
        "   \"connections\": 400\n",
        "}\n",
        "\"\"\".split(\"\\n\")\n",
        "\n",
        "# SAVE THE CONFIG\n",
        "!mkdir -p /root/.config/peerflix-server/\n",
        "open(configPath, 'w').close()\n",
        "\n",
        "configFile = open(configPath, \"a+\")\n",
        "\n",
        "for line in data:\n",
        "  configFile.write(line + \"\\n\")\n",
        "\n",
        "configFile.close()\n",
        "\n",
        "if os.path.isfile(\"/tools/node/bin/peerflix-server\") == False:\n",
        "  os.system('npm install -g peerflix-server')\n",
        "else:\n",
        "  !pkill peerflix-server\n",
        "  \n",
        "if os.path.isfile(\"/usr/bin/autossh\") == False:\n",
        "    os.system('sudo apt update && sudo apt install -y autossh')\n",
        "\n",
        "if os.path.isfile(\"/usr/local/bin/ngrok\") == False:\n",
        "  !wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n",
        "  !unzip -qq -n ngrok-stable-linux-amd64.zip\n",
        "  !mv ngrok /usr/local/bin/ngrok\n",
        "  !rm -f /content/ngrok-stable-linux-amd64.zip\n",
        "  clear_output(wait=True)\n",
        "else:\n",
        "  !pkill ngrok\n",
        "  \n",
        "print(\"Copy authtoken from https://dashboard.ngrok.com/auth\")\n",
        "authtoken = getpass.getpass()\n",
        "get_ipython().system_raw('/usr/local/bin/ngrok authtoken $authtoken && /usr/local/bin/ngrok tcp 4445 &')\n",
        "\n",
        "#Opening peerflix-server in background\n",
        "!mkdir -p peerflix \n",
        "!PORT=4445 nohup peerflix-server &\n",
        "clear_output(wait=True)\n",
        "#Get public address and print connect command\n",
        "time.sleep(10)\n",
        "with urllib.request.urlopen('http://localhost:4040/api/tunnels') as response:\n",
        "  data = json.loads(response.read().decode())\n",
        "  (host, port) = data['tunnels'][0]['public_url'][6:].split(':')\n",
        "  print(f'Open this link in new tab http://{host}:{port}')\n",
        "\n",
        "#MEGAcmd installing\n",
        "if os.path.isfile(\"/usr/bin/mega-cmd\") == False:\n",
        "  print(\"Installing Mega ...\")\n",
        "  os.system('sudo apt-get -y update && sudo apt-get -y install libmms0 libc-ares2 libc6 libcrypto++6 libgcc1 libmediainfo0v5 libpcre3 libpcrecpp0v5 libssl1.1 libstdc++6 libzen0v5 zlib1g apt-transport-https && sudo curl -sL -o/var/cache/apt/archives/MEGAcmd.deb https://mega.nz/linux/MEGAsync/Debian_9.0/amd64/megacmd-Debian_9.0_amd64.deb && sudo dpkg -i /var/cache/apt/archives/MEGAcmd.deb && sudo ldconfig')\n",
        "  print(\"Mega is installed.\")\n",
        "else:\n",
        "  !pkill mega-cmd\n",
        "\n",
        "#Google Drive CLI Client\n",
        "if os.path.isfile(\"/usr/local/bin/gdrive\") == False:\n",
        "  print(\"Google Drive Cli Client installing ...\")\n",
        "  !wget -q -c -nc https://github.com/gdrive-org/gdrive/releases/download/2.1.0/gdrive-linux-x64\n",
        "  !mv gdrive-linux-x64 /usr/local/bin/gdrive\n",
        "  !sudo chmod +x /usr/local/bin/gdrive\n",
        "  !rm -f /content/gdrive-linux-x64\n",
        "  print(\"Google drive client installed\")\n",
        "else:\n",
        "  !pkill gdrive\n",
        "print(\"All Done.\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KplycKPzw3yw",
        "colab_type": "code",
        "cellView": "form",
        "colab": {}
      },
      "source": [
        "\n",
        "#@markdown <br><center><img src='https://www.iconsdb.com/icons/preview/red/warning-xxl.png' height=\"50\" alt=\"netdata\"/></center>\n",
        "#@markdown <br><center><h1>CLEAN DOWNLODAS FOLDER</center>\n",
        "#@markdown <br><center>PEERFLIX-SERVER don't delete removed torrent task file. So you do this manually after remove torrent task.\n",
        "!touch /content/downloads/errorRemove\n",
        "!cd downloads/ && sudo rm -r *\n",
        "print(\"All Deleted on /content/peerflix\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "c3cOMk38t8Zm",
        "colab_type": "text"
      },
      "source": [
        "<img src='http://pngimages.net/sites/default/files/zip-file-png-image-15464.png' height=\"30\" alt=\"netdata\"/>\n",
        "\n",
        "#Extracte Zip"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aK-ly9_92Chr",
        "colab_type": "code",
        "cellView": "form",
        "colab": {}
      },
      "source": [
        "Cloud_torrent = True #@param {type:\"boolean\"}\n",
        "Peerflix = False #@param {type:\"boolean\"}\n",
        "#@markdown <br><center><img src='https://cdn0.iconfinder.com/data/icons/mattress-1/512/mattress-pad-bed-01-512.png' height=\"100\" alt=\"netdata\"/></center>\n",
        "#@markdown <center><h2>Extracte Zip</h2></center><br>\n",
        "\n",
        "if Cloud_torrent:\n",
        "  !cd downloads/ && find . -name '*.zip' -exec sh -c 'unzip -d \"`dirname \\\"{}\\\"`\" \"{}\" && sudo rm \"`dirname \\\"{}\\\"`\" \"{}\"' ';'\n",
        "else:\n",
        "  print(\"Not enabled Cloud_torrent. Skipping....\")\n",
        "if Peerflix:  \n",
        "  !cd peerflix/ && find . -name '*.zip' -exec sh -c 'unzip -d \"`dirname \\\"{}\\\"`\" \"{}\" && sudo rm \"`dirname \\\"{}\\\"`\" \"{}\"' ';'\n",
        "else:\n",
        "  print(\"Not enabled Peerflix. Skipping....\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yOnMe4O3jtXf",
        "colab_type": "text"
      },
      "source": [
        "<img src=\"https://upload.wikimedia.org/wikipedia/commons/a/ad/Logo_of_Google_Drive.svg\" weight=\"100px\" height=\"40px\" />\n",
        "\n",
        "#TRANSFERE to Google drive\n",
        "<font color='red'>I recommend to use native drive mount and use drag and drop to transfer data to google drive.</font> [Read more.](https://colab.research.google.com/drive/1srw_HFWQ2SMgmWIawucXfusGzrj1_U0q#scrollTo=UADu4vFNkjX1)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ljfomn66wMz3",
        "colab_type": "code",
        "cellView": "form",
        "colab": {}
      },
      "source": [
        "#@markdown <h3>⬅️ Click Here to</h3>\n",
        "\n",
        "#@markdown #INPUT YOUR GOOGLE DRIVE authtoken\n",
        "#@markdown #### (SOMETIMES WRITING ISSUES. SO RUN AGAIN)\n",
        "\n",
        "!gdrive about\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HGNUf02Lj2Sg",
        "colab_type": "code",
        "cellView": "form",
        "colab": {}
      },
      "source": [
        "Cloud_torrent = True #@param {type:\"boolean\"}\n",
        "Peerflix = False #@param {type:\"boolean\"}\n",
        "#UPLOAD TO GOOGLE DRIVE\n",
        "#@markdown <br><center><img src='https://support.questetra.com/wp-content/uploads/2018/04/googledrive-simpleupload-fi-en.png' height=\"200\" alt=\"netdata\"/></center>\n",
        "#@markdown <center><h2>TRANSFERE NOW</h2></center><br>\n",
        "\n",
        "if Cloud_torrent:\n",
        "  !gdrive upload --recursive /content/downloads\n",
        "else:\n",
        "  print(\"Not enabled Cloud_torrent. Skipping....\")\n",
        "if Peerflix:\n",
        "  !gdrive upload --recursive /content/peerflix\n",
        "else:\n",
        "  print(\"Not enabled Peerflix. Skipping....\")\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kSvUMUuTPX7r",
        "colab_type": "text"
      },
      "source": [
        "![alt text](https://mega.nz/favicon.ico?v=3) \n",
        "#TRANSFERE to MEGA\n",
        "\n",
        "---\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "q3uC2Lo5R6N9",
        "colab_type": "code",
        "cellView": "both",
        "colab": {}
      },
      "source": [
        "#@markdown <center><h2>INPUT YOUR MEGA ID</h2></center><br>\n",
        "#@markdown <center>(If you see Fetching nodes 100% on stuck, rerun)</center><br>\n",
        "\n",
        "import os\n",
        "import contextlib\n",
        "import subprocess\n",
        "import time\n",
        "\n",
        "#INPUT YOUR MEGA ID\n",
        "\n",
        "USERNAME = \"\" #@param {type:\"string\"}\n",
        "PASSWORD = \"\" #@param {type:\"string\"}\n",
        "# Unix, Windows and old Macintosh end-of-line\n",
        "newlines = ['\\n', '\\r\\n', '\\r']\n",
        "def unbuffered(proc, stream='stdout'):\n",
        "    stream = getattr(proc, stream)\n",
        "    with contextlib.closing(stream):\n",
        "        while True:\n",
        "            out = []\n",
        "            last = stream.read(1)\n",
        "            # Don't loop forever\n",
        "            if last == '' and proc.poll() is not None:\n",
        "                break\n",
        "            while last not in newlines:\n",
        "                # Don't loop forever\n",
        "                if last == '' and proc.poll() is not None:\n",
        "                    break\n",
        "                out.append(last)\n",
        "                last = stream.read(1)\n",
        "            out = ''.join(out)\n",
        "            yield out\n",
        "\n",
        "def example():\n",
        "    cmd = ['mega-login', USERNAME, PASSWORD]\n",
        "    proc = subprocess.Popen(\n",
        "        cmd,\n",
        "        stdout=subprocess.PIPE,\n",
        "        stderr=subprocess.STDOUT,\n",
        "        # Make all end-of-lines '\\n'\n",
        "        universal_newlines=True,\n",
        "    )\n",
        "    for line in unbuffered(proc):\n",
        "        clear_output(wait=True)\n",
        "        print(line)\n",
        "example()\n",
        "!mega-whoami\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "E3N6hyM2I6hk",
        "colab_type": "code",
        "cellView": "form",
        "colab": {}
      },
      "source": [
        "Cloud_torrent = True #@param {type:\"boolean\"}\n",
        "Peerflix = False #@param {type:\"boolean\"}\n",
        "#@markdown <br><center><img src='https://www.impactcomputing.co.uk/uploads/files/services/managed-office-365.svg' height=\"100\" alt=\"netdata\"/></center>\n",
        "#@markdown <center><h2>TRANSFERE NOW</h2></center><br>\n",
        "#TRANSFERE NOW\n",
        "\n",
        "import contextlib\n",
        "import subprocess\n",
        "import time\n",
        "\n",
        "\n",
        "# Unix, Windows and old Macintosh end-of-line\n",
        "newlines = ['\\n', '\\r\\n', '\\r']\n",
        "def unbuffered(proc, stream='stdout'):\n",
        "    stream = getattr(proc, stream)\n",
        "    with contextlib.closing(stream):\n",
        "        while True:\n",
        "            out = []\n",
        "            last = stream.read(1)\n",
        "            # Don't loop forever\n",
        "            if last == '' and proc.poll() is not None:\n",
        "                break\n",
        "            while last not in newlines:\n",
        "                # Don't loop forever\n",
        "                if last == '' and proc.poll() is not None:\n",
        "                    break\n",
        "                out.append(last)\n",
        "                last = stream.read(1)\n",
        "            out = ''.join(out)\n",
        "            yield out\n",
        "\n",
        "def example():\n",
        "    if Cloud_torrent:\n",
        "      cmd = ['mega-put', 'downloads', '/colab']\n",
        "    if Peerflix:\n",
        "      cmd = ['mega-put', 'peerflix', '/colab']\n",
        "      \n",
        "    proc = subprocess.Popen(\n",
        "        cmd,\n",
        "        stdout=subprocess.PIPE,\n",
        "        stderr=subprocess.STDOUT,\n",
        "        # Make all end-of-lines '\\n'\n",
        "        universal_newlines=True,\n",
        "    )\n",
        "    for line in unbuffered(proc):\n",
        "        clear_output(wait=True)\n",
        "        print(line)\n",
        "\n",
        "example()"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}